//
//  ClassCollectionView.m
//  Cat
//
//  Created by lushitong on 2018/12/31.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "ClassCollectionView.h"
#import "HttpAssottManager.h"
#import "ShopCollectionCell.h"
#import "DetailViewController.h"

@interface ClassCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>

@property (nonatomic, strong) NSNumber *productID;
@property (nonatomic, strong) NSMutableArray *productListArray;
@property (nonatomic, strong) NSString *sortType; // 排序方式

@end

@implementation ClassCollectionView

- (instancetype)initWithFrame:(CGRect)frame layout:(UICollectionViewFlowLayout *)layout withProductID:(NSNumber *)productID {
	if (self = [super initWithFrame:frame layout:layout]) {
		self.addRefresh = YES;
		self.needLoadMore = YES;
		self.emheaderHeight = - NavigationBarHeight;
		self.collectionView.backgroundColor = [UIColor whiteColor];
		[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
			make.edges.equalTo(@0);
		}];
		
		self.appType = QKAppTypeQingka;
		
		[self.collectionView registerClass:[ShopCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([ShopCollectionCell class])];
		self.productID = productID;
		self.sortType = @"01";
	}
	return self;
}

- (void)sendRequestWithIndexType:(NSString *)type {
	[self.productListArray removeAllObjects];
	self.sortType = type;
	/*
	 01是综合排序、
	 02是优惠券排序、
	 03是返佣排序、
	 04是券后价从高到低、
	 05是券后价从低到高、
	 06是销量从高到低、
	 07是销量从低到高
	 */
	self.pageIndex = 1;
	[self sendRequest];
}

- (void)sendRequest {
	if (self.pageIndex == 1) {
		[self.productListArray removeAllObjects];
	}
	
	[HttpAssottManager getCategoryProductListWithCategoryID:self.productID andPageIndex:@(self.pageIndex) andSortType:self.sortType withSuccess:^(NSArray *productList) {
		[self.productListArray addObjectsFromArray:productList];
		[self.collectionView reloadData];
		[self finishLoadData:productList];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
	return UIEdgeInsetsMake(0, kQKFitModuleDecimal(10.0), 0, kQKFitModuleDecimal(10.0));
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
	return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	return self.productListArray.count;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
	CGFloat width = (ScreenWidth - kQKFitModuleDecimal(24 + 9)) / 2;
	return CGSizeMake(width,width + kQKFitModuleDecimal(94));
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	ShopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ShopCollectionCell class]) forIndexPath:indexPath];
	NSArray *array = self.productListArray;
	[cell setModel:[array safeObjectAtIndex:indexPath.row]];
	return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	[collectionView deselectItemAtIndexPath:indexPath animated:YES];
	NSArray *array = self.productListArray;
	GroomsModel *mode = [array safeObjectAtIndex:indexPath.row];
	DetailViewController *de = [[DetailViewController alloc] init];
	de.proudctID = mode.productId;
	[self.currentNavigationController pushViewController:de animated:YES];
}

#pragma mark----- lazy init -----

- (NSMutableArray *)productListArray {
	if (!_productListArray) {
		_productListArray = [[NSMutableArray alloc] init];
	}
	return _productListArray;
}

@end
